using TCC.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace TCC.Framework
{
    /// <summary>
    /// MVVM数据基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BindDataBase<T>
    {
        protected List<UIBehaviour> m_UIs;

        private T _data;

        public T Data
        {
            get => _data;
            set
            {
                _data = value;
                DataValueChangeEvent();
            }
        }

        /// <summary>
        /// 数据改变事件
        /// </summary>
        protected abstract void DataValueChangeEvent();

        /// <summary>
        /// 绑定UI
        /// </summary>
        /// <param name="uI"></param>
        public virtual void Bind(UIBehaviour uI)
        {
            m_UIs.Add(uI);
            BindEvent(uI);
        }

        protected abstract void BindEvent(UIBehaviour uI);

        protected virtual void Bind(IEnumerable<UIBehaviour> uIs)
        {
            foreach (var item in uIs)
                Bind(item);
        }

        public BindDataBase()
        {
            m_UIs = new List<UIBehaviour>();
        }

        public BindDataBase(UIBehaviour uI)
        {
            m_UIs = new List<UIBehaviour>();
            Bind(uI);
        }

        public BindDataBase(IEnumerable<UIBehaviour> uIs)
        {
            m_UIs = new List<UIBehaviour>();
            Bind(uIs);
        }
    }

    public class BindIntData : BindDataBase<int>
    {
        public static implicit operator int(BindIntData data)=>data.Data;
        public static explicit operator float(BindIntData data)=>data.Data;

        protected override void BindEvent(UIBehaviour uI)
        {
            uI.AddSliderOnValueChanged((value) => Data = (int)value);
            
        }

        protected override void DataValueChangeEvent()
        {
            foreach (var item in m_UIs)
            {
                item.SetTextStr(Data.ToString());
            }
        }
    }
}
